package com.ccc.boot.base;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ccc.boot.pojo.Query;
import com.ccc.boot.response.ResponseCode;
import com.ccc.boot.response.ResponseResult;
import com.ccc.boot.validation.Add;
import com.ccc.boot.validation.Update;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 通用请求处理控制器
 *
 * @author superC
 * @date 2022/3/24
 * @version 0.1.0
 */
@SuppressWarnings("all")
public class BaseController<T extends BaseDO, S extends IBaseService<T>> {

    protected static final Logger logger = LoggerFactory.getLogger(BaseController.class);

    @Resource
    protected HttpServletRequest request;

    @Resource
    protected HttpServletResponse response;

    @Autowired
    protected S service;

    /**
     * 添加
     *  // @Validated  Spring扩展的校验器，支持组校验
     *
     * @param entity 实体对象
     * @return {@link ResponseResult}
     */
    @PostMapping()
    public ResponseResult insert(@Validated(value = {Add.class}) @RequestBody T entity) {
        // 业务逻辑
        boolean created = service.insert(entity);
        if (created) {
            return ResponseResult.success("添加成功");
        }
        return ResponseResult.failure(ResponseCode.DATA_ADD_FAILURE);
    }

    /**
     * 删除
     *
     * @param id {@code Long}
     * @return {@link ResponseResult}
     */
    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable Long id) {
        // 业务逻辑
        boolean deleted = service.delete(id);
        if (deleted) {
            return ResponseResult.success(ResponseCode.DATA_DELETE_SUCCESS);
        }
        return ResponseResult.failure(ResponseCode.DATA_DELETE_FAILURE);
    }

    /**
     * 编辑
     *
     * @param entity 实体对象
     * @return {@link ResponseResult}
     */
    @PutMapping()
    public ResponseResult update(@Validated(value = {Update.class}) @RequestBody T entity) {
        // 业务逻辑
        boolean updated = service.update(entity);
        if (updated) {
            return ResponseResult.success("编辑成功");
        }
        return ResponseResult.failure(ResponseCode.DATA_UPDATE_FAILURE);
    }

    /**
     * 根据ID，获取详情信息
     *
     * @param id {@code Long}
     * @return {@link ResponseResult}
     */
    @GetMapping("/{id}")
    public ResponseResult get(@PathVariable Long id) {
        T entity = service.get(id);
        return ResponseResult.success("获取数据成功", entity);
    }

    /**
     * 分页
     *
     * @param params    包含page, pageSize, 以及其他查询条件字段集，key为字段名，value为其对应的值
     * @return {@link ResponseResult}
     */
    @GetMapping("/pageList")
    public ResponseResult pageList(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        IPage<?> iPage = service.page(query);
        return ResponseResult.success("获取数据成功", iPage);
    }

}